package com.ivan.hospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ivan.common.result.Result;
import com.ivan.model.entity.hosp.Department;
import com.ivan.hospital.service.DepartmentService;
import com.ivan.model.vo.hosp.DepartmentQueryVo;
import com.ivan.model.vo.hosp.DepartmentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * @Author      Ivan
 * @Date        2023/4/4 20:37
 * @Description
 */
@Api(tags = "医院科室管理接口")
//@CrossOrigin//使用gateway后做了配置
@RestController
@RequestMapping("/admin/hosp/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "条件查询医院科室带分页")
    @GetMapping("/findByPage/{current}/{limit}")
    public Result findByPage(@PathVariable("current") long current,
                             @PathVariable("limit") long limit,
                             DepartmentQueryVo queryVo) {
        Page<Department> page = departmentService.findByPage(current, limit, queryVo);
        return Result.ok(page);
    }

    @ApiOperation(value = "根据id查询医院科室")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Long id) {
        Department department = departmentService.findById(id);
        return Result.ok(department);
    }

    @ApiOperation(value = "添加医院科室")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(@RequestBody Department department) {
        int save = departmentService.saveDepartment(department);
        if (save > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据id删除医院科室")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        int delete = departmentService.removeById(id);
        if (delete > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据idList批量删除医院科室")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Long> idList) {
        int delete = departmentService.removeByIds(idList);
        if (delete > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "修改医院科室")
    @PostMapping("/updateDepartment")
    public Result updateDepartment(@RequestBody Department department) {
        int update = departmentService.updateDepartment(department);
        if (update > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }




    @ApiOperation(value = "根据hascode查询所有科室")
    @GetMapping("/getDeptListByHascode/{hoscode}")
    public Result getDeptListByHascode(@PathVariable("hoscode") String hoscode) {
        List<DepartmentVo> departmentVos = departmentService.getDeptListByHascode(hoscode);
        return Result.ok(departmentVos);
    }
}
